#include "lvgl.h"
#include "img_def.h"
#include "lv_common_function.h"
 
LV_ATTRIBUTE_LARGE_CONST static const lv_img_dsc_t* start_logo_src[][2] =
{
    {IMG_IMAGE0_1, IMG_IMAGE0_2}, {IMG_IMAGE1_1, IMG_IMAGE1_2}, {IMG_IMAGE2_1, IMG_IMAGE2_2}, {IMG_IMAGE3_1, IMG_IMAGE3_2}, {IMG_IMAGE4_1, IMG_IMAGE4_2},
    {IMG_IMAGE5_1, IMG_IMAGE5_2}, {IMG_IMAGE6_1, IMG_IMAGE6_2}, {IMG_IMAGE7_1, IMG_IMAGE7_2}, {IMG_IMAGE8_1, IMG_IMAGE8_2}, {IMG_IMAGE9_1, IMG_IMAGE9_2},
    {IMG_IMAGE10_1, IMG_IMAGE10_2}, {IMG_IMAGE11_1, IMG_IMAGE12_2}, {IMG_IMAGE13_1, IMG_IMAGE13_2}, {IMG_IMAGE14_1, IMG_IMAGE14_2}, {IMG_IMAGE15_1, IMG_IMAGE15_2},
    {IMG_IMAGE16_1, IMG_IMAGE16_2}, {IMG_IMAGE17_1, IMG_IMAGE17_2}, {IMG_IMAGE18_1, IMG_IMAGE18_2}, {IMG_IMAGE19_1, IMG_IMAGE19_2}, {IMG_IMAGE20_1, IMG_IMAGE20_2},
    {IMG_IMAGE21_1, IMG_IMAGE21_2}, {IMG_IMAGE22_1, IMG_IMAGE22_2}, {IMG_IMAGE23_1, IMG_IMAGE23_2}, {IMG_IMAGE24_1, IMG_IMAGE24_2}, {IMG_IMAGE25_1, IMG_IMAGE25_2},
    {IMG_IMAGE26_1, IMG_IMAGE26_2}, {IMG_IMAGE27_1, IMG_IMAGE27_2}, {IMG_IMAGE28_1, IMG_IMAGE28_2}, {IMG_IMAGE29_1, IMG_IMAGE29_2}, {IMG_IMAGE30_1, IMG_IMAGE30_2},
    {IMG_IMAGE31_1, IMG_IMAGE31_2}, {IMG_IMAGE32_1, IMG_IMAGE32_2}, {IMG_IMAGE33_1, IMG_IMAGE33_2}, {IMG_IMAGE34_1, IMG_IMAGE34_2}, {IMG_IMAGE35_1, IMG_IMAGE35_2},
    {IMG_IMAGE36_1, IMG_IMAGE36_2}, {IMG_IMAGE37_1, IMG_IMAGE37_2}, {IMG_IMAGE38_1, IMG_IMAGE38_2}, {IMG_IMAGE39_1, IMG_IMAGE39_2}, {IMG_IMAGE40_1, IMG_IMAGE40_2},
    {IMG_IMAGE41_1, IMG_IMAGE41_2}, {IMG_IMAGE42_1, IMG_IMAGE42_2}, {IMG_IMAGE43_1, IMG_IMAGE43_2}, {IMG_IMAGE44_1, IMG_IMAGE44_2}, {IMG_IMAGE45_1, IMG_IMAGE45_2},
    {IMG_IMAGE46_1, IMG_IMAGE46_2}, {IMG_IMAGE47_1, IMG_IMAGE47_2}, {IMG_IMAGE48_1, IMG_IMAGE48_2}, {IMG_IMAGE49_1, IMG_IMAGE49_2}, {IMG_IMAGE50_1, IMG_IMAGE50_2},
    {IMG_IMAGE51_1, IMG_IMAGE51_2}, {IMG_IMAGE52_1, IMG_IMAGE52_2}, {IMG_IMAGE53_1, IMG_IMAGE53_2}, {IMG_IMAGE54_1, IMG_IMAGE54_2}, {IMG_IMAGE55_1, IMG_IMAGE55_2},
    {IMG_IMAGE56_1, IMG_IMAGE56_2}, {IMG_IMAGE57_1, IMG_IMAGE57_2}, {IMG_IMAGE58_1, IMG_IMAGE58_2}, 
};

 
static void switch_page(lv_timer_t *t)
{
    static uint8_t cnt=0;
    ++cnt;
    cnt%=5;
	lv_obj_t * obj = (lv_obj_t *)t->user_data;
	if (!lv_obj_is_valid(obj))
		return;
    lv_obj_scroll_to_view(lv_obj_get_child(obj,cnt),LV_ANIM_OFF);
	printf("switch_page\r\n");
}

//static void page_del_cb(lv_event_t *e)
//{
//	if(e->user_data!=NULL)
//	{
//		lv_timer_del(e->user_data);
//		printf("page_del_cb\r\n");
//        e->user_data = NULL;
//	}
//}

static void page_event_handler(lv_event_t *e)
{
	lv_event_code_t event = lv_event_get_code(e);
	lv_obj_t *obj = lv_event_get_target(e);
	if(!lv_obj_is_valid(obj))
        return;
	if(event == LV_EVENT_DELETE)
	{
		if(e->user_data!=NULL)
		{
			lv_timer_del(e->user_data);
			printf("page_del_cb\r\n");
	        e->user_data = NULL;
		}
	}else if(event == LV_EVENT_KEY)
	{
		uint32_t *key = lv_event_get_param(e);
        switch(*key)
        {
        case KEY1_CODE:
				 
             break;
        case KEY3_CODE:
			 
             break;
        case KEY5_CODE: //return
				if(e->user_data!=NULL)
				{
					lv_timer_del(e->user_data);
					printf("page_del_cb\r\n");
					e->user_data = NULL;
				}
				user_jump_page(fr_list_page);
            break;
        }
		printf("key_code %d \n", *key);
	}

}

void fr_test_page_func(lv_obj_t* parent,lv_point_t *top)
{
    if(!lv_obj_is_valid(parent))
        return;
    UI_PARENT_INIT(parent);
	lv_obj_t *obj_cont = lv_obj_create(parent);
	lv_obj_remove_style_all(obj_cont);
	lv_obj_set_size(obj_cont, LV_PCT(100), LV_PCT(100));
	lv_obj_set_scroll_snap_x(obj_cont, LV_SCROLL_SNAP_CENTER);
	lv_obj_set_flex_flow(obj_cont,LV_FLEX_FLOW_ROW);
	lv_obj_set_scrollbar_mode(obj_cont,LV_SCROLLBAR_MODE_OFF);
	lv_obj_add_flag(obj_cont,LV_OBJ_FLAG_SCROLL_ONE);
    lv_obj_t *obj0 = lv_obj_create(obj_cont);
	fr_lv_86box_page1_func(obj0,NULL);	
	
    lv_obj_t *obj1 = lv_obj_create(obj_cont);
	fr_lv_86box_page2_func(obj1,NULL);	
	
    lv_obj_t *obj2 = lv_obj_create(obj_cont);
	fr_lv_instrument_panel_func(obj2,NULL);

    lv_obj_t *obj3 = lv_obj_create(obj_cont);
	fr_lv_instrument_panel_km_func(obj3,NULL);
	
    lv_obj_t *obj4 = lv_obj_create(obj_cont);
    fr_lv_app_music_control_func(obj4,NULL); 

//    lv_obj_t *obj5 = lv_obj_create(obj_cont);
//  	fr_list_page(obj5,NULL);
    lv_timer_t * time = lv_timer_create(switch_page,2000,obj_cont);
	
	lv_obj_add_event_cb(parent,page_event_handler,LV_EVENT_ALL,(void*)time);
	
}
extern volatile uint8_t lock_timer_flag;
void logo_timer_cb(lv_timer_t* t)
{
	static uint8_t index = 0;
	lv_obj_t * obj = (lv_obj_t *)t->user_data;
	if (!lv_obj_is_valid(obj))
		return;


	lock_timer_flag = 1;
	lv_img_set_src(lv_obj_get_child(obj,0),start_logo_src[index][0]);
	lv_img_set_src(lv_obj_get_child(obj,1),start_logo_src[index][1]);
	if(++index>57)
	{
		index = 0;
		lock_timer_flag = 0;
		lv_timer_del(t);
		user_jump_page(fr_list_page);
	}
	
}

void fr_logo_page_func(lv_obj_t* parent,lv_point_t *top)
{
   if(!lv_obj_is_valid(parent))
        return;
    UI_PARENT_INIT(parent);
    lv_obj_set_style_bg_color(parent,lv_color_make(0xff,0xff,0xff),0);
 
 	lv_obj_t *img_cont = lv_obj_create(parent);
 	lv_obj_remove_style_all(img_cont);
 	lv_obj_set_size(img_cont,200,200);
    lv_obj_set_style_bg_color(img_cont,lv_color_make(0xff,0xff,0xff),0);
    lv_obj_align(img_cont,LV_ALIGN_CENTER,0,0);
    lv_obj_t *old_obj=NULL;
 	// lv_obj_add_event_cb(img_cont,ui_custom_cards_click_event_cb,LV_EVENT_LONG_PRESSED,NULL);
 	for (uint8_t j = 0; j < 2; j++)
 	{
 		lv_obj_t *img= lv_img_create(img_cont);
 		lv_img_set_src(img,start_logo_src[0][j]);
 		if(j == 0)
 		{
 			lv_obj_set_pos(img,0,0);
 		}
 		else
 		{
			lv_obj_align_to(img,old_obj,LV_ALIGN_OUT_BOTTOM_LEFT,0,0);
 		}
 		old_obj = img;
    }
	lv_timer_create(logo_timer_cb,20,img_cont);
  
}
 
